// pages/grade/grade.j
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttons: [{
      id: 1,
      name: '一年级'
    }, {
      id: 2,
      name: '二年级'
    }, {
      id: 3,
      name: '三年级'
    }, {
      id: 4,
      name: '四年级'
    }, {
      id: 5,
      name: '五年级'
    }, {
      id: 6,
      name: '六年级'
    }, {
      id: 7,
      name: '七年级'
    }, {
      id: 8,
      name: '八年级'
    }, {
      id: 9,
      name: '九年级'
    }, {
      id: 10,
      name: '高一'
    }, {
      id: 11,
      name: '高二'
    }, {
      id: 12,
      name: '高三'
    }],

    terms: [{
      id: 1,
      name: '上学期'
    }, {
      id: 12,
      name: '下学期'
    }],
    currentGrade: "",
    currentTerm: ""

  },
  updateInfo() {
    var that = this;
    wx.request({
      url: 'http://localhost:8080/eb-back-web/updateGrade',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        grade: that.data.currentGrade,
        term: that.data.currentTerm,
        uid: getApp().globalData.uid
      },
      success: function (res) {
        console.log(res.data);
        app.globalData.grade = that.data.currentGrade;
        app.globalData.term = that.data.currentTerm;
        wx.navigateBack({
          delta: 1  //返回上一页
        })
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
      },


      fail: function (res) {
        console.log(res.data);
        wx.navigateBack({
          delta: 1  //返回上一页面
        })
        wx.showToast({
          title: '修改失败！',
          icon: 'fail',
          duration: 2000
        })
      }
    })
  },
  radioButtonTap: function(e) {
    console.log(e)
    let id = e.target.dataset.id
    console.log(id)
    for (let i = 0; i < this.data.buttons.length; i++) {
      if (this.data.buttons[i].name == id) {
        //当前点击的位置为true即选中
        this.data.buttons[i].checked = true;
      } else {
        //其他的位置为false
        this.data.buttons[i].checked = false;
      }
    }
    this.setData({
      buttons: this.data.buttons,
      currentGrade: id
    })
  },


  radioButtonTap2: function(te) {
    console.log(te)
    let tid = te.target.dataset.id
    console.log(tid)
    for (let i = 0; i < this.data.terms.length; i++) {
      if (this.data.terms[i].name == tid) {
        //当前点击的位置为true即选中
        this.data.terms[i].checked = true;
      } else {
        //其他的位置为false
        this.data.terms[i].checked = false;
      }
    }
    this.setData({
      terms: this.data.terms,
      currentTerm: tid
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.buttons[0].checked = true;
    this.setData({
      buttons: this.data.buttons,
      currentGrade:this.data.buttons[0].name
    })
    this.data.terms[0].checked = true;
    this.setData({
      terms: this.data.terms,
      currentTerm:this.data.terms[0].name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
